package com.bootdo.system.dao;

import com.bootdo.system.domain.CustomerQrCode;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface CustomerQrCodeMapper {

	@Select("select * from tb_customer_qrcode where id = #{id}")
	CustomerQrCode get(Long id);
	
	@Select("select * from tb_customer_qrcode where customerId = #{customerId}  order by id desc limit 1 ")
	CustomerQrCode getByCustomerId(Long customerId);
	
	@Select("<script>" +
	"select * from tb_customer_qrcode " + 
			"<where>" + 
			 "<if test=\"id != null and id != ''\">"+ "and id = #{id} " + "</if>" + 
	  		  "<if test=\"customerId != null and customerId != ''\">"+ "and customerId = #{customerId} " + "</if>" + 
		  	"</where>"+ 
			" <choose>" + 
	            "<when test=\"sort != null and sort.trim() != ''\">" + 
	                "order by ${sort} ${order}" + 
	            "</when>" + 
				"<otherwise>" + 
	                "order by id desc" + 
				"</otherwise>" + 
	        "</choose>"+
			"</script>")
	List<CustomerQrCode> list(Map<String,Object> map);
	
	
	
	@Insert("insert into tb_customer_qrcode (`customerId`, `imgStr`)"
	+ "values (#{customerId}, #{imgStr})")
	@Options(useGeneratedKeys=true)
	int save(CustomerQrCode code);
	
	@Update("<script>"+ 
			"update tb_customer_qrcode " + 
					"<set>" + 
		            "<if test=\"id != null\">`id` = #{id}, </if>" + 
                    "<if test=\"customerId != null\">`customerId` = #{customerId}, </if>" + 
                    "<if test=\"imgStr != null\">`imgStr` = #{imgStr}, </if>" + 
          			"</set>" + 
					"where id = #{id}"+
			"</script>")
	int update(CustomerQrCode code);
	
	@Delete("delete from tb_customer_qrcode where id =#{id}")
	int remove(Long id);
	
	@Delete("delete from tb_customer_qrcode where customerId =#{customerId}")
	int removeByCustomerId(Long customerId);
	
	@Delete("<script>"+ 
			"delete from tb_customer_qrcode where id in " + 
					"<foreach item=\"id\" collection=\"array\" open=\"(\" separator=\",\" close=\")\">" + 
						"#{id}" + 
					"</foreach>"+
			"</script>")
	int batchRemove(Integer[] ids);
}
